﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using doveData;

namespace WindowsFormsApp1
{
    public partial class Form1 : Form
    {


        [DllImport("Kernel32.dll")]
        private static extern bool AllocConsole(); //启动窗口  
        [DllImport("kernel32.dll", EntryPoint = "FreeConsole")]
        private static extern bool FreeConsole();      //释放窗口，即关闭  
        private float dist = 10;

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            AutoCompleteStringCollection ac = new AutoCompleteStringCollection();
            foreach (string name in StatContrl.NameDict.Keys)
            {
                ac.Add(name);
            }
           
            this.textBox1.AutoCompleteCustomSource = ac;
            this.textBox2.AutoCompleteCustomSource = ac;
            this.textBox3.AutoCompleteCustomSource = ac;
            this.textBox3.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
            this.textBox3.AutoCompleteSource = AutoCompleteSource.CustomSource;

            ac.Clear();
            foreach (string s in StatContrl.NearNameList)
            {
                ac.Add(s);
            }
            this.textBox4.AutoCompleteMode=AutoCompleteMode.Suggest;
            this.textBox4.AutoCompleteSource=AutoCompleteSource.CustomSource;
            this.textBox4.AutoCompleteCustomSource = ac;
        }


        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            testView1.ResetPath();
            testView1.Invalidate();
        }
        /// <summary>
        /// 查询最少换乘按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button1_Click(object sender, EventArgs e)
        {
            testView1.ResetPath();
            string outstring = "";
            List<Station> tempList = new List<Station>();
            if (StatContrl.NameDict.ContainsKey(textBox1.Text) && StatContrl.NameDict.ContainsKey(textBox2.Text))
            {
               
                tempList = StatContrl.GetLeastChange(textBox1.Text, textBox2.Text);
                for (int i = 0; i < tempList.Count; i++)
                {
                    testView1.SelectedLineList.Add(tempList[i]);
                    if (i!=0)
                    {
                        foreach (string line in tempList[i].LineDict.Keys)
                        {
                            if (tempList[i-1].LineDict.ContainsKey(line))
                            {
                                outstring += "乘坐" + line + "号线\t" + tempList[i - 1].Name + "---->" + tempList[i].Name+"\n";
                            }
                        }
                    }
                }
            }
            outstring +="换乘次数："+ (testView1.SelectedLineList.Count - 2).ToString();
            testView1.Invalidate();
            MessageBox.Show(outstring);

        }


        /// <summary>
        /// 查询周边按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button2_Click(object sender, EventArgs e)
        {

            foreach (Vector2 pos in StatContrl.W_CheckStationLocation(textBox3.Text,dist))
            {
                
            }
            MessageBox.Show(StatContrl.W_CheckStation(textBox3.Text, dist));
        }
        /// <summary>
        /// 后台
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button3_Click(object sender, EventArgs e)
        {
            AllocConsole();
            ConsoleControl.Intro();
            //TODO
            //添加后台控制代码
            FreeConsole();
        }

        private void panel1_Paint(object sender, PaintEventArgs e)
        {

        }

        private void panel1_Paint_1(object sender, PaintEventArgs e)
        {

        }
        /// <summary>
        /// 最少用时
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button4_Click(object sender, EventArgs e)
        {
            testView1.ResetPath();
            string outstring = "";
            LinePath leastPath = new LinePath();
            List<Station> list = new List<Station>();
            if (StatContrl.NameDict.ContainsKey(textBox1.Text) && StatContrl.NameDict.ContainsKey(textBox2.Text))
            {
                leastPath = StatContrl.AllStation.W_GetShortestPath_DIJ(StatContrl.NameDict[textBox1.Text], StatContrl.NameDict[textBox2.Text]);
            }
            for (int i = 0; i < leastPath.Items.Count; i++)
            {
                list.Add((Station)leastPath.Items[i]);
            }
            if (list.Count == 0)
            {
                MessageBox.Show("输入站点错误！");
                return;
            }
            testView1.SelectedLineList = list;
            float distance = leastPath.LengthOfPath;
            float velocity = 80;
            float Time = 0;
            Time = 2 * distance / velocity * 60;
            testView1.isPath = true;
            testView1.Invalidate();
            MessageBox.Show("预计使用时间为：" + (int)Time + "分钟!");
        }

        private void textBox2_TextChanged(object sender, EventArgs e)
        {
            testView1.ResetPath();
            testView1.Invalidate();
        }
        /// <summary>
        /// 最少用钱
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button5_Click(object sender, EventArgs e)
        {
            testView1.ResetPath();
            string outstring = "";
            LinePath leastPath = new LinePath();
            List<Station> list = new List<Station>();
            if (StatContrl.NameDict.ContainsKey(textBox1.Text) && StatContrl.NameDict.ContainsKey(textBox2.Text))
            {
                leastPath = StatContrl.AllStation.W_GetShortestPath_DIJ(StatContrl.NameDict[textBox1.Text], StatContrl.NameDict[textBox2.Text]);
            }
            for (int i = 0; i < leastPath.Items.Count; i++)
            {
                list.Add((Station)leastPath.Items[i]);
            }
            if (list.Count == 0)
            {
                MessageBox.Show("输入站点错误！");
                return;
            }
            testView1.SelectedLineList = list;
            float distance = leastPath.LengthOfPath;
            float money = 0;
            //Distance每两个单位为一公里
            if (distance < 8)
            {
                money = 2;
            }
            else if (distance >= 8 && distance < 24)
            {
                money = (float)Math.Floor((distance - 8) / 8);
                money += 2;
            }
            else if (distance >= 24 && distance < 48)
            {
                money = (float)Math.Floor((distance - 24) / 12);
                money += 4;
            }
            else
            {
                money = (float)Math.Floor((distance - 48) / 16);
                money += 6;
            }
            testView1.isPath = true;
            testView1.Invalidate();
            MessageBox.Show("预计使用金钱为：" + (int)money + "元");

        }

        private void textBox3_TextChanged(object sender, EventArgs e)
        {

        }
        /// <summary>
        /// 查看最近周边
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button7_Click(object sender, EventArgs e)
        {
            MessageBox.Show(StatContrl.W_CheckStationNearest(textBox3.Text,dist));
            
        }

        private void textBox4_TextChanged(object sender, EventArgs e)
        {


        }
        /// <summary>
        /// 最好酒店
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button6_Click(object sender, EventArgs e)
        {
            MessageBox.Show(StatContrl.W_FindBestScoreHotel());
        }
        /// <summary>
        /// 查周边
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button8_Click(object sender, EventArgs e)
        {
            string outstring = "";
            for (int i = 1; i < 4; i++)
            {
                outstring += StatContrl.W_FindStationByNear(textBox4.Text, i);
            }
            MessageBox.Show(outstring);
        }


    }
}
